using System;
using System.Text;
using System.Windows.Forms;
using ctsu.BaseForms;
using ctsu.CustomControls;
//using Chb.ctsu.Data;

namespace ctsu.Admin.BusLogic
{
	public class BLFormGridAncillaryRateImpl : BLFormGridBaseImpl
	{
		public BLFormGridAncillaryRateImpl(FormGridMeta formGridMeta, UserGrid uGrid, FormInfoBase formInfo, Form form)
			: base(formGridMeta, uGrid, formInfo, form)
		{

		}

		private bool VerifyRate(FormGridColumnMeta fgcm, string text, string row)
		{
			StringBuilder sb = new StringBuilder();

			int iRow = Convert.ToInt32(row);
			string col = fgcm.DisplayName;
			string errMsgKey = row + ":" + col;

			if (this._errMessages.Contains(errMsgKey))
				this._errMessages.Remove(errMsgKey);

			if (text.Length == 0)
			{
				if (fgcm.IsRequired)
				{
					sb.Append("Row:" + row + "," + "Col:" + col);
					sb.Append(Environment.NewLine);
					sb.Append("  " + fgcm.DisplayName + " is a required field");
					this._errMessages.Add(errMsgKey, sb.ToString());
					return false;
				}
			}

			decimal val = Convert.ToDecimal(text);
			if (val < 0)
			{
				sb.Append("Row:" + row + "," + "Col:" + col);
				sb.Append(Environment.NewLine);
				sb.Append("  " + fgcm.DisplayName + " must be positive.");
				this._errMessages.Add(errMsgKey, sb.ToString());
				return false;
			}
			return true;
		}

		private bool VerifyStartDate(FormGridColumnMeta fgcm, string text, string row)
		{
			StringBuilder sb = new StringBuilder();

			int iRow = Convert.ToInt32(row);
			string col = fgcm.DisplayName;
			string errMsgKey = row + ":" + col;

			if (this._errMessages.Contains(errMsgKey))
				this._errMessages.Remove(errMsgKey);

			if (text.Length == 0)
			{
				if (fgcm.IsRequired)
				{
					sb.Append("Row:" + row + "," + "Col:" + col);
					sb.Append(Environment.NewLine);
					sb.Append("  " + fgcm.DisplayName + " is a required field");
					this._errMessages.Add(errMsgKey, sb.ToString());
					return false;
				}
			}
			DateTime dt = Convert.ToDateTime(text);
			//check against previous end date
			if (iRow == 1)
				return true;

			string prevText = this._uGrid.Grid[iRow - 1, 3].CellValue.ToString();
			if (prevText.Length == 0)
			{
				sb.Append("Row:" + (iRow - 1) + "," + "Col: End Date");
				sb.Append(Environment.NewLine);
				sb.Append("Previous end date cannot be blank!");
				this._errMessages.Add(errMsgKey, sb.ToString());
				return false;
			}

			DateTime dtEnd = Convert.ToDateTime(prevText);
			if (dt.Date.CompareTo(dtEnd.Date) < 1)
			{
				sb.Append("Row:" + row + "," + "Col:" + col);
				sb.Append(Environment.NewLine);
				sb.Append("Start date must be greater than previous end date!");
				this._errMessages.Add(errMsgKey, sb.ToString());
				return false;
			}
			return true;
		}

		private bool VerifyEndDate(FormGridColumnMeta fgcm, string text, string row)
		{
			StringBuilder sb = new StringBuilder();

			int iRow = Convert.ToInt32(row);
			string col = fgcm.DisplayName;
			string errMsgKey = row + ":" + col;

			if (this._errMessages.Contains(errMsgKey))
				this._errMessages.Remove(errMsgKey);

			if (text.Length == 0)
			{
				if (fgcm.IsRequired)
				{
					sb.Append("Row:" + row + "," + "Col:" + col);
					sb.Append(Environment.NewLine);
					sb.Append("  " + fgcm.DisplayName + " is a required field");
					this._errMessages.Add(errMsgKey, sb.ToString());
					return false;
				}
				else
				{
					//if this is the last row than a blank end date is ok
					if (iRow == this._uGrid.Grid.RowCount)
						return true;
					else
					{
						sb.Append("Row:" + row + "," + "Col:" + col);
						sb.Append(Environment.NewLine);
						sb.Append("A previous end date cannot be blank!");
						this._errMessages.Add(errMsgKey, sb.ToString());
						return false;
					}
				}
			}
			//if it's not the last date
			DateTime dt = Convert.ToDateTime(text);

			//end date cannot be greater than start date
			string startText = this._uGrid.Grid[iRow, 2].CellValue.ToString();
			if (startText.Length == 0)
			{
				sb.Append("Row:" + (iRow - 1) + "," + "Col: Start Date");
				sb.Append(Environment.NewLine);
				sb.Append("Start date cannot be blank!");
				this._errMessages.Add(errMsgKey, sb.ToString());
				return false;
			}

			DateTime dtStart = Convert.ToDateTime(startText);
			if (dt.Date.CompareTo(dtStart.Date) < 1)
			{
				sb.Append("Row:" + row + "," + "Col:" + col);
				sb.Append(Environment.NewLine);
				sb.Append("Start date must be greater than end date!");
				this._errMessages.Add(errMsgKey, sb.ToString());
				return false;
			}

			//check against previous end date
			if (iRow == this._uGrid.Grid.RowCount)
				return true;

			string nextText = this._uGrid.Grid[iRow + 1, 2].CellValue.ToString();

			if (nextText.Length == 0)
			{
				sb.Append("Row:" + (iRow + 1) + "," + "Col: Start Date");
				sb.Append(Environment.NewLine);
				sb.Append("The next start date cannot be blank!");
				this._errMessages.Add(errMsgKey, sb.ToString());
				return false;
			}
			DateTime dtNext = Convert.ToDateTime(nextText);
			if (dt.Date.CompareTo(dtNext.Date) > 0)
			{
				sb.Append("Row:" + row + "," + "Col:" + col);
				sb.Append(Environment.NewLine);
				sb.Append("End date must not be greater than next start date!");
				this._errMessages.Add(errMsgKey, sb.ToString());
				return false;
			}
			return true;
		}
	}
}
